#pragma once

#include <iostream>
#include <pthread.h>

namespace Lock
{
    class mutex
    {
    private:
        pthread_mutex_t _lock;

    public:
        // 锁是禁止拷贝的以及移动赋值
        mutex(const mutex &) = delete;
        const mutex &operator=(const mutex &) = delete;
        mutex()
        {
            //锁本质上是一个二元信息量sem
            int n=pthread_mutex_init(&_lock,nullptr);
            (void)n;//忽略返回值
        }
        ~mutex()
        {
            int n=pthread_mutex_destroy(&_lock);
            (void)n;
        }
        void lock()
        {
            int n=pthread_mutex_lock(&_lock);
            (void)n;
        }
        void unlock()
        {
            int n=pthread_mutex_unlock(&_lock);
            (void)n;
        }
        pthread_mutex_t*adder()
        {
            return &_lock;
        }
    };
    //主要实现Rall思想；
    class LockGuard
    {
    public:
        LockGuard(mutex &mtx):_mtx(mtx)
        {
            _mtx.lock();
        }
        ~LockGuard()
        {
            _mtx.unlock();
        }
    private:
        mutex &_mtx;
    };
}


